/**
 * 
 */
package com.and.xmppsdk.util;

import android.content.Context;
import android.media.AudioManager;
import android.os.Vibrator;

/**
 * 
 * 辅助功能类,按钮震动，按钮声音.
 * 
 * @author dai.rui.lin
 * 
 */
public final class AssistHeper {

	public static boolean audio_flag = true;
	public static boolean vibrator_flag = true;

	public static void openAudio(Context context) {
		AudioManager am = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		// 设置声音模式，NORMAL（普通）
		am.setMode(AudioManager.MODE_NORMAL);
		int mVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC) / 2;
		// 设置音量大小
		am.setStreamVolume(AudioManager.STREAM_MUSIC, mVolume, 0);
		am.adjustVolume(AudioManager.ADJUST_RAISE, 0);
		vibrator_flag = true;
	}

	public static void closeAudio(Context context) {
		AudioManager am = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		// 设置成静音模式
		am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
		vibrator_flag = false;
	}

	/**
	 * @param context
	 *            上下文
	 * @param isOpen
	 *            是否振动
	 */
	public static void openVibrator(Context context, boolean isOpen) {
		if (vibrator_flag) {
			if (isOpen) {
				Vibrator mVibrator = (Vibrator) context
						.getSystemService(Context.VIBRATOR_SERVICE);
				mVibrator.vibrate(new long[] { 10, 300 }, -1);
			}
		}
	}

	/**
	 * 
	 * 打开听筒模式
	 * 
	 * @param context
	 * @param isOpen
	 *            是否打开
	 */
	public static void openEarphone(Context context, boolean isOpen) {
		AudioManager audiomanager = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
			if (isOpen) {
				audiomanager.setMode(AudioManager.MODE_IN_CALL);
			} else {
				audiomanager.setMode(AudioManager.MODE_NORMAL);
			}
	}
}
